<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\lose\model;

use think\Model;
use think\Db;


class WechatNewsModel extends Model
{

    // protected $type = [
    //     'more' => 'array',
    // ];

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    public function adminList($filter)
    {
        $appid = db('wechat_public_number')->value('appid');

        $where = [
            'a.create_time' => ['>=', 0],
            'a.delete_time' => 0,
            'a.appid'       => $appid,
        ];

        //审核状态
        if( !empty($filter['audit_status']) ){
            $where['a.audit_status'] = $filter['audit_status'];
        }

        $field = '*';
        
        //开始时间
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        //结束时间
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }

        //关键字
        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.title'] = ['like', "%$keyword%"];
        }

        $list = $this
             -> alias('a')
             -> field($field)
             -> where($where)
             -> order($filter['sort_by'], $filter['list_order'])
             -> paginate(10);

        // echo $this->getLastSql();
        // die;
        // pre($articles,1);
        return $list;

    }
    
}
